package com.mc.moring.main.modle

import androidx.lifecycle.MutableLiveData
import com.mc.moring.app.LogUtils
import com.mc.moring.base.BaseApplication
import com.mc.moring.base.BaseModle
import com.mc.moring.base.HttpCallback
import com.mc.moring.bean.WeblinkConfigBean
import com.mc.moring.netconfig.Apis
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.ResponseBody


class PermisosModle :BaseModle(){

    private val  coroutineScope = CoroutineScope(Dispatchers.Default)
    val weblinkConfigBean: MutableLiveData<WeblinkConfigBean?> = MutableLiveData()

    companion object{
        val instant by lazy (LazyThreadSafetyMode.SYNCHRONIZED){
            PermisosModle()
        }
    }

    fun getZiLiPageWebLinkConfig(map: MutableMap<String,String>,httpCallback: HttpCallback){
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiSplashServer.getWebLinkConfig(request)
                val responseBody: ResponseBody? = data.body()
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun getWebPageContent(map: MutableMap<String,String>,httpCallback: HttpCallback) {
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiSplashServer.getWebPageContent(request)
                val responseBody: ResponseBody? = data.body()
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun getHead():Map<String,Any>{
        val app_id = BaseApplication.getInstance().packageName
        val headMap = HashMap<String,Any>()
        headMap["App_id"] = "com.mc.moring"
        headMap["App_name"] = "MONI CASH"
        headMap["User_Agent"] = "Retrofit-Sample-App"
        headMap["App_version"] = "1.0.0"
        headMap["accept-language"] = "es"
        headMap["Authorization"] = ""
        return headMap
    }

    fun getUnreadMessageCount(map: MutableMap<String,String>,httpCallback: HttpCallback) {
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiSplashServer.getUnreadMessageCount(request)
                val responseBody: ResponseBody? = data.body()
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e UnreadMessage---：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e UnreadMessage--- ：$e")
            }
        }
    }

    fun getStationMessageList(map: MutableMap<String,String>,httpCallback: HttpCallback){
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiSplashServer.getStationMessageList(request)
                val responseBody: ResponseBody? = data.body()
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun getLoanList(map: MutableMap<String,String>,httpCallback: HttpCallback){
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiSplashServer.getLoanList(request)
                val responseBody: ResponseBody? = data.body()
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun getCashFlow(map: MutableMap<String,String>,httpCallback: HttpCallback){
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiSplashServer.getCashFlow(request)
                val responseBody: ResponseBody? = data.body()
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }

    fun deleteBankAccount(map: MutableMap<String,String>,httpCallback: HttpCallback){
        coroutineScope.launch {
            try {
                var result = ""
                val mapString = getZiLiPageJson(map, false)
                val request: RequestBody =
                    mapString.toRequestBody("application/json".toMediaTypeOrNull())
                val data = Apis.apiSplashServer.deleteBankAccount(request)
                val responseBody: ResponseBody? = data.body()
                result = decryptingZiLiPageData(data)
                LogUtils.logE("e ：$result")
                httpCallback.onSuccess(result)
            } catch (e: Exception) {
                LogUtils.logE("e ：$e")
            }
        }
    }
}